package hikari.gui.panels.particle;

import com.gooddies.events.ValueChangedEvent;
import com.gooddies.swing.RowLayout;
import com.gooddies.texteditors.DefaultFloatTextField;
import hikari.gui.guicomponents.hLabel;
import hikari.math.CircleDrawer;
import hikari.persistence.file.FileSection;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import org.swig.jirr.IParticleEmitter;
import org.swig.jirr.IParticleRingEmitter;
import org.swig.jirr.IParticleSystemSceneNode;
import org.swig.jirr.IVideoDriver;
import org.swig.jirr.vector3df;

/**
 * @author sad
 */
public class RingEmitterEditorPanel extends EmitterEditorPanel {

    private IParticleRingEmitter emitter;
    private DefaultFloatTextField radius;
    private DefaultFloatTextField thickness;
    private Vector3dEditor center;
    private vector3df ringNormalVector = vector3df.DIR_UP;

    public RingEmitterEditorPanel() {
        setTitle("Ring Emitter");

        setLayout(new RowLayout().setStartYGap(20).setStartXGap(10).setEndYGap(10));
        add(new hLabel("Ring Radius"));
        radius = new DefaultFloatTextField(0, 999999);
        radius.setPreferredSize(new Dimension(80, 20));
        radius.setValueChangedEventNumber(new ValueChangedEvent<Float>() {
            @Override
            public void valueChanged(Float value) {
                radiusChanged(value);
            }
        });
        add(radius, RowLayout.NewRow);
        add(new hLabel("Ring Thickness"));
        thickness = new DefaultFloatTextField(0, 999999);
        thickness.setPreferredSize(new Dimension(80, 20));
        thickness.setValueChangedEventNumber(new ValueChangedEvent<Float>() {
            @Override
            public void valueChanged(Float value) {
                thicknessChanged(value);
            }
        });
        add(thickness, RowLayout.NewRow);

        add(new hLabel("Ring Position"), RowLayout.NewRow);
        center = new Vector3dEditor(this);
        center.setValueChangedEvent(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                centerChanged();
            }
        });
    }

    private void radiusChanged(float newRadius) {
        emitter.setRadius(newRadius);
    }

    private void thicknessChanged(float newThickness) {
        emitter.setRingThickness(newThickness);
    }

    private void centerChanged() {
        vector3df pos = center.getValue();
        emitter.setCenter(pos);
    }

    @Override
    public void putFromEmmiter(IParticleEmitter particleEmitter) {
        this.emitter = (IParticleRingEmitter) particleEmitter;
        center.setValue(this.emitter.getCenter());
        radius.setValue(this.emitter.getRadius());
        thickness.setValue(this.emitter.getRingThickness());
    }

    @Override
    public void putToEmmiter(IParticleEmitter emmiter) {
    }

    @Override
    public void renderEmitter(IVideoDriver driver) {
        final float coeficient = 0.714285714f;//this coeficient is for radius of emmiter and radius in system
        driver.setMaterial(getMaterial());
        vector3df tCenter = emitter.getCenter();
        float tRadius = emitter.getRadius() / coeficient;
        float tThickness = emitter.getRingThickness() / coeficient;
        float halfThick = tThickness / 2;
        CircleDrawer drawer = new CircleDrawer(40, tCenter, ringNormalVector, tRadius + halfThick);
        drawer.drawCircle();
        drawer = new CircleDrawer(40, tCenter, ringNormalVector, tRadius - halfThick);
        drawer.drawCircle();
    }

    public static void writeData(FileSection parentSection, IParticleEmitter emitter) throws IOException {
        IParticleRingEmitter spEmitter = (IParticleRingEmitter) emitter;
        FileSection sect = parentSection.createNewSection("ringEm");
        sect.writeVector("center", spEmitter.getCenter());
        sect.writeFloat("radius", spEmitter.getRadius());
        sect.writeFloat("thick", spEmitter.getRingThickness());
        sect.close();
    }

    public static IParticleRingEmitter loadData(IParticleSystemSceneNode partSystem, FileSection parentSection) throws IOException {
        IParticleRingEmitter emitter = partSystem.createRingEmitter(vector3df.NULLVECTOR, 1, 1);
        FileSection settings = parentSection.readSection(parentSection.getChunk("ringEm"));
        vector3df center = settings.readVector(settings.getChunk("center"));
        float radius = settings.readFloat(settings.getChunk("radius"));
        float thick = settings.readFloat(settings.getChunk("thick"));

        emitter.setCenter(center);
        emitter.setRadius(radius);
        emitter.setRingThickness(thick);

        partSystem.setEmitter(emitter);
        settings.close();
        return emitter;
    }
}